import Vue from 'vue'
import Router from 'vue-router'

// 导入路由组件
import First from '@/components/first'
import Firstone from '@/components/firstone'
import Firsttwo from '@/components/firsttwo'
import Second from '@/components/second'
import Third from '@/components/third'
import Login from '@/components/login'
import Order from '@/components/order'
Vue.use(Router)
    //得到路由对象
let router = new Router({
        // 解决方案多个路由控制多个视图
        routes: [{
                path: '/',
                name: '/',
                // 配置路由视图
                components: {
                    default: First,
                    abc: Second,
                    xyz: Third
                }
            },
            {
                path: "/first",
                name: "First",
                component: First,
                children: [{
                        path: "firstone",
                        name: "Firstone",
                        component: Firstone
                    },
                    {
                        path: "firsttwo",
                        name: "Firsttwo",
                        component: Firsttwo
                    },
                ]
            },
            {
                path: "/second",
                name: "Second",
                component: Second
            },
            {
                path: "/third",
                name: "Third",
                component: Third
            },
            {
                path: "/login",
                name: "Login",
                component: Login
            },
            {
                path: "/order",
                name: "Order",
                component: Order
            }
        ],
        // 设置路由切换方式
        mode: 'history'
    })
    // 设置守卫路由
    // 前置钩子
router.beforeEach((to, from, next) => {
    console.log("路由前执行");
    // 如果访问的是不需要登录的，直接放行
    if (to.path == '/' || to.path == '/login') {
        // 放行
        next();
        return;
    }
    // 判断是否登录了 假设未登录
    let flag = false;
    if (!flag) {
        alert("请先登录");
        // 跳转至登录页面
        next("/login");
    } else {
        // 放行
        next();
    }

});

router.afterEach((to, from, next) => {
    console.log("路由后执行");
});
// 导出路由
export default router;